/******************************************************************************
@file:  FoodMenuHome.java
@author Ramesh Anandhan

DESCRIPTION
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
******************************************************************************/

package com.smartc.foodmenu;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.smartc.connection.HttpConnection;
import com.smartc.ui.AccountLogin;
import com.smartc.ui.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class FoodMenuHome extends Activity implements OnClickListener{

	protected static final String TAG = "FoodMenuHome";
	
	private Spinner mLocation = null;
	private Spinner mBranch = null;
	private Button mBtnMenu = null;
	private String mSelectedLocation = null;
	private String mSelectedBranch = null;
	
	private HttpConnection mCityConnection;
	private ResponseHandler mResponse = new ResponseHandler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.foodmenu_home);
		mCityConnection = new HttpConnection();
		String domain = getResources().getString(R.string.domain);
		String query = getResources().getString(
				R.string.url_food_menu_location_list);
		String url = domain + query;

		mCityConnection.setUrl(url);
		mCityConnection.clearParam();
		mCityConnection.AddHandler(mResponse);
		mCityConnection.Execute();
		
		mLocation = (Spinner) findViewById(R.id.fmenu_location);
		mBranch = (Spinner) findViewById(R.id.fmenu_branch);
		mBtnMenu = (Button) findViewById(R.id.fmenu_get_menu);
		mBtnMenu.setOnClickListener(this);
		mLocation.setOnItemSelectedListener(new OnItemSelectedListener() {
			@SuppressWarnings("unchecked")
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (AccountLogin.DEBUG)
					Log.i(TAG, "onItemSelected() position:" + position);

				if (position == 0){
					mSelectedLocation = null;
					mSelectedBranch = null;
					mBranch.setEnabled(false);
					String empty[] = new String[0];
					mBranch.setAdapter(new ArrayAdapter(FoodMenuHome.this,
							R.layout.simple_spinner_item, empty));
					return;
				}
				mSelectedLocation = mResponse.mLocation_list_id.get(--position);

				String domain = getResources().getString(R.string.domain);
				String query = getResources().getString(
						R.string.url_food_menu_branch_list);
				String url = domain + query;

				mCityConnection.setUrl(url);
				mCityConnection.clearParam();
				if (AccountLogin.DEBUG)
					Log.i(TAG, "onItemSelected() idLocation:" + mSelectedLocation);
				mCityConnection.AddParam("idLocation", mSelectedLocation);
				mCityConnection.Execute();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}

		});
		
		mBranch.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (AccountLogin.DEBUG)
					Log.i(TAG, "onItemSelected() position:" + position);

				if (position == 0){
					mSelectedBranch = null;
					return;
				}
				mSelectedBranch = mResponse.mBranch_list_id.get(--position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}

		});
	}
	
	public class ResponseHandler extends DefaultHandler {

		private boolean idLocation = false;
		//private boolean Locations = false;
		private boolean nmLocation = false;
		
		//private boolean Branches = false;
		//private boolean branch = false;
		private boolean idBranch = false;
		private boolean nmBranch = false;
		
		public ArrayList<String> mLocation_list = new ArrayList<String>();
		public ArrayList<String> mLocation_list_id = new ArrayList<String>();
		public ArrayList<String> mBranch_list = new ArrayList<String>();
		public ArrayList<String> mBranch_list_id = new ArrayList<String>();
		
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (this.idLocation) {
				mLocation_list_id.add(new String(ch, start, length));
			}else if (this.nmLocation) {
				mLocation_list.add(new String(ch, start, length));
			}else if (this.idBranch) {
				mBranch_list_id.add(new String(ch, start, length));
			}else if (this.nmBranch) {
				mBranch_list.add(new String(ch, start, length));
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if (localName.equals("idLocation")) {
				this.idLocation = false;
			}else if (localName.equals("Locations")) {
				//this.Locations = false;
				Message myMessage = new Message();
				myMessage.obj = "LOCATION";
				handler.sendMessage(myMessage);
			}else if (localName.equals("nmLocation")) {
				this.nmLocation = false;
			}else if (localName.equals("Branches")) {
				//this.Branches = false;
				Message myMessage = new Message();
				myMessage.obj = "BRANCH";
				handler.sendMessage(myMessage);
			}else if (localName.equals("branch")) {
				//this.branch = false;
			}else if (localName.equals("idBranch")) {
				this.idBranch = false;
			}else if (localName.equals("nmBranch")) {
				this.nmBranch = false;
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (localName.equals("idLocation")) {
				this.idLocation = true;
			}else if (localName.equals("Locations")) {
				//this.Locations = true;
				mLocation_list_id.clear();
				mLocation_list.clear();
				mLocation_list.add("Select Location");
			}else if (localName.equals("nmLocation")) {
				this.nmLocation = true;
			}else if (localName.equals("Branches")) {
				//this.Branches = true;
				mBranch_list_id.clear();
				mBranch_list.clear();
				mBranch_list.add("Select Branch");
			}else if (localName.equals("branch")) {
				//this.branch = true;
			}else if (localName.equals("idBranch")) {
				this.idBranch = true;
			}else if (localName.equals("nmBranch")) {
				this.nmBranch = true;
			}
		}
	}

	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage msg:" + (String) msg.obj);
			String loginmsg = (String) msg.obj;
			if (loginmsg.equals("LOCATION")) {
				int count = mResponse.mLocation_list.size();
				String location[] = new String[count];
				for (int i = 0; i < count; i++)
					location[i] = mResponse.mLocation_list.get(i);

				ArrayAdapter Adapter = new ArrayAdapter(FoodMenuHome.this,
						R.layout.simple_spinner_item, location);
				mLocation.setAdapter(Adapter);
				mSelectedLocation = null;
				mSelectedBranch = null;
				String empty[] = new String[0];
				mBranch.setAdapter(new ArrayAdapter(FoodMenuHome.this,
						R.layout.simple_spinner_item, empty));
			}else if (loginmsg.equals("BRANCH")) {
				int count = mResponse.mBranch_list.size();
				String branch[] = new String[count];
				for (int i = 0; i < count; i++)
					branch[i] = mResponse.mBranch_list.get(i);

				ArrayAdapter Adapter = new ArrayAdapter(FoodMenuHome.this,
						R.layout.simple_spinner_item, branch);
				mBranch.setAdapter(Adapter);
				mBranch.setEnabled(true);
				mSelectedBranch = null;
			}
		}
	};
	@Override
	public void onClick(View view) {
		if(view.getId() == R.id.fmenu_get_menu){
			if(validateInput() == false)
				return;
			Intent intent = new Intent();
			intent.setClass(FoodMenuHome.this, MenuItems.class);
			if(AccountLogin.DEBUG) Log.i(TAG,"onClick mSelectedBranch:"+mSelectedBranch);
			intent.putExtra("idBranch", mSelectedBranch);
			startActivity(intent);
		}
	}
	
	private boolean validateInput(){
		boolean valid = true;
		if(mSelectedLocation == null){
			Toast.makeText(this, "select location", Toast.LENGTH_SHORT).show();
			valid = false;
		}
			
		if(mSelectedBranch == null){
			Toast.makeText(this, "select branch", Toast.LENGTH_SHORT).show();
			valid = false;
		}
		return valid;
	}

}
